import * as Cesium from 'cesium';

class FlexAutoBox {
  constructor(viewer) {
    this.viewer = viewer;
    this.transparentBlue = new Cesium.Color(0.0, 0.0, 1.0, 0.1);
    this.blackColor = Cesium.Color.BLACK;
    this.FlexBoxPrimitive = [];
    // 存储模型矩阵，避免重复计算
    this.modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(Cesium.Cartesian3.fromDegrees(110, 40, 0));
    this.cubeSize = 15.0;
    this.sxzyqhArr = [0, 0, 0, 0, 0, 0]
    this.baseMatrix = [
      0, 1, 2, 0, 2, 3,   // 底面
      4, 5, 6, 4, 6, 7,   // 顶面(4,4,4,4,4,4)
      0, 1, 5, 0, 5, 4,   // 前面(0,0,3,0,3,1)
      1, 2, 6, 1, 6, 5,   // 右面(1,1,4,1,4,2)
      2, 3, 7, 2, 7, 6,   // 后面(2,2,5,2,5,3)
      3, 0, 4, 3, 4, 7,   // 左面(3,-1,2,3,2,4)
    ]
    this.base8Pos64Arr = [
      0.0, 0.0, 0.0,                                // 0
      this.cubeSize, 0.0, 0.0,                      // 1
      this.cubeSize, this.cubeSize, 0.0,            // 2
      0.0, this.cubeSize, 0.0,                      // 3
      0.0, 0.0, this.cubeSize,                      // 4
      this.cubeSize, 0.0, this.cubeSize,            // 5
      this.cubeSize, this.cubeSize, this.cubeSize,  // 6
      0.0, this.cubeSize, this.cubeSize,            // 7
    ]
    this.currentMatrixIndexs = []
    this.currentPos64Arr = []
    this.topDiff = [
      // 不用底面
      [4, 4, 4, 4, 4, 4],
      [0, 0, 3, 0, 3, 1],
      [1, 1, 4, 1, 4, 2],
      [2, 2, 5, 2, 5, 3],
      [3, -1, 2, 3, 2, 4]
    ]
    // 方向枚举
    this.direction0D = ['0_0_0']
    this.direction1D = ['-1_0_0', '0_-1_0', '0_0_-1', '0_0_1', '0_1_0', '1_0_0']
    this.direction2D = ['-1_-1_0', '-1_0_-1', '-1_0_1', '-1_1_0', '0_-1_-1', '0_-1_1', '0_1_-1', '0_1_1', '1_-1_0', '1_0_-1', '1_0_1', '1_1_0']
    this.direction3D = ['-1_-1_-1', '-1_-1_1', '-1_1_-1', '-1_1_1', '1_-1_-1', '1_-1_1', '1_1_-1', '1_1_1']
  }
  // 上、下、左、右、前、后
  addFlexBox (dir = '0_0_0') {
    // 异常包裹下
    try {
      dir = '0_0_1'
      // 减少方向的遍历
      const index = dir.split('0').length - 1 || 0
      const directionArr = this[`direction${index}D`]
      console.log('当前的移动方向->', dir, directionArr);
      // 清空之前的盒子
      this.viewer.scene.primitives.remove(this.FlexBoxPrimitive[0]);
      this.viewer.scene.primitives.remove(this.FlexBoxPrimitive[1]);
      this.FlexBoxPrimitive = this.drawBox(this.sxzyqhArr[0])
      this.sxzyqhArr[0]++
      this.FlexBoxPrimitive.forEach(element => {
        this.viewer.scene.primitives.add(element);
      });
    } catch (e) {
      console.error(); ('异常', e);
    }
  }
  // 绘制一个5面体
  drawBox (n) {
    // 动态计算顶点坐标
    const arr = [
      0.0, 0.0, this.cubeSize * n,                        // 8
      this.cubeSize, 0.0, this.cubeSize * n,              // 9
      this.cubeSize, this.cubeSize, this.cubeSize * n,    // 10
      0.0, this.cubeSize, this.cubeSize * n,              // 11
    ]
    // 动态计算顶点索引
    const arr2 = [
      // 4, 5, 6, 4, 6, 7,   // 底面
      8, 9, 10, 8, 10, 11,   // 顶面
      4, 5, 9, 4, 9, 8,      // 前面
      5, 6, 10, 5, 10, 9,    // 右面
      6, 7, 11, 6, 11, 10,   // 后面
      7, 4, 8, 7, 8, 11,     // 左面
    ]
    const matPos = this.base8Pos64Arr.concat(arr)
    console.log('顶点坐标->', matPos);
    const matIndex = this.baseMatrix.concat(arr2)
    console.log('顶点索引->', matIndex);
    // 定义立方体的顶点坐标
    const pos64Arr = new Float64Array(matPos);
    // 绘制点
    this.drawPoint()
    // 线的外观
    const linepma = new Cesium.PerInstanceColorAppearance({
      flat: true,
      translucent: false,
      renderState: {
        depthTest: { enabled: true },
        lineWidth: Math.min(5.0, this.viewer.scene.maximumAliasedLineWidth),
      },
      material: Cesium.Material.fromType('Color', { color: Cesium.Color.GRAY })
    })
    // console.log('线外观->', linepma);
    // 面的外观
    const appearance = new Cesium.PerInstanceColorAppearance({
      flat: true,
      translucent: true
    });
    // console.log('盒子外观->', appearance);
    // 创建 Primitive 并添加到场景
    return [
      // 绘制边线
      new Cesium.Primitive({ geometryInstances: this.drawSideLineMatrix(this.cubeSize * n, matIndex), appearance: linepma, modelMatrix: this.modelMatrix, asynchronous: false }),
      // 绘制平面
      new Cesium.Primitive({ geometryInstances: this.drawFixedFaceMatrix(pos64Arr, matIndex), appearance: appearance, asynchronous: false })
    ]
  }
  // 生成矩阵的点
  drawPoint () {

  }
  // 生成矩阵的边线
  drawSideLineMatrix (startZ) {
    const size = this.cubeSize;
    const vertices = [
      new Cesium.Cartesian3(0.0, 0.0, 0.0),             // 0
      new Cesium.Cartesian3(size, 0.0, 0.0),            // 1
      new Cesium.Cartesian3(size, size, 0.0),           // 2
      new Cesium.Cartesian3(0.0, size, 0.0),            // 3
      new Cesium.Cartesian3(0.0, 0.0, startZ),      // 4
      new Cesium.Cartesian3(size, 0.0, startZ),     // 5
      new Cesium.Cartesian3(size, size, startZ),    // 6
      new Cesium.Cartesian3(0.0, size, startZ),     // 7
    ];
    const edges = [
      [0, 1], [1, 2], [2, 3], [3, 0], // Bottom face
      // [4, 5], [5, 6], [6, 7], [7, 4], // Top face
      // [0, 4], [1, 5], [2, 6], [3, 7]  // Vertical edges
    ];
    let pos64Arr = [];
    edges.forEach((edge) => {
      let start = vertices[edge[0]];
      let end = vertices[edge[1]];
      pos64Arr.push(start.x, start.y, start.z, end.x, end.y, end.z);
    });
    console.log('边线坐标->', pos64Arr);
    const geometryAttribute = new Cesium.GeometryAttribute({
      componentDatatype: Cesium.ComponentDatatype.DOUBLE,
      componentsPerAttribute: 3, // x, y, z per vertex
      values: new Float64Array(pos64Arr)
    });
    let mygeometry = new Cesium.Geometry({
      attributes: {
        position: geometryAttribute
      },
      primitiveType: Cesium.PrimitiveType.LINES,
      boundingSphere: Cesium.BoundingSphere.fromVertices(pos64Arr)
    });
    mygeometry.boundingSphere = Cesium.BoundingSphere.fromVertices(pos64Arr);
    return new Cesium.GeometryInstance({
      geometry: mygeometry, attributes: {
        color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromCssColorString('#F7F7F7'))
      }
    });
  }
  // 生成矩阵面
  drawFixedFaceMatrix (pos64Arr, matIndexArr) {
    // 定义分组数量
    const positions = new Cesium.GeometryAttribute({
      componentDatatype: Cesium.ComponentDatatype.DOUBLE,
      componentsPerAttribute: 3,
      values: pos64Arr,
    });
    // 定义立方体的面（由顶点索引组成）
    const indices = new Uint16Array(matIndexArr);
    // 创建几何体
    const geometry = new Cesium.Geometry({
      attributes: { position: positions },
      indices: indices,
      primitiveType: Cesium.PrimitiveType.TRIANGLES,
      boundingSphere: Cesium.BoundingSphere.fromVertices(pos64Arr),
    });
    // 创建几何实例
    return new Cesium.GeometryInstance({
      geometry: geometry,
      modelMatrix: this.modelMatrix,
      attributes: { color: Cesium.ColorGeometryInstanceAttribute.fromColor(this.transparentBlue) }
    });
  }
}
export default FlexAutoBox;
